package com.heer.imagepick;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by heer on 15/11/16.
 */
public class TitleBuilder {
    private View mViewTitle;
    private TextView mTvTitle;
    private TextView mTvRight;
    private TextView mTvLeft;
    private ImageView mIvRight;
    private ImageView mIvLeft;

    public TitleBuilder(Activity context) {
        mViewTitle = context.findViewById(R.id.rl_titlebar);
        mIvLeft = (ImageView) mViewTitle.findViewById(R.id.titlebar_iv_l);
        mIvRight = (ImageView) mViewTitle.findViewById(R.id.titlebar_iv_r);
        mTvLeft = (TextView) mViewTitle.findViewById(R.id.titlebar_tv_l);
        mTvRight = (TextView) mViewTitle.findViewById(R.id.titlebar_tv_r);
        mTvTitle = (TextView) mViewTitle.findViewById(R.id.title);
    }

    public TitleBuilder(View view) {
        mViewTitle = view.findViewById(R.id.rl_titlebar);
        mIvLeft = (ImageView) mViewTitle.findViewById(R.id.titlebar_iv_l);
        mIvRight = (ImageView) mViewTitle.findViewById(R.id.titlebar_iv_r);
        mTvLeft = (TextView) mViewTitle.findViewById(R.id.titlebar_tv_l);
        mTvRight = (TextView) mViewTitle.findViewById(R.id.titlebar_tv_r);
        mTvTitle = (TextView) mViewTitle.findViewById(R.id.title);
    }

    //title
    public TitleBuilder setTitleText(String text) {
        mTvTitle.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        mTvTitle.setText(text);
        return this;
    }

    //left
    public TitleBuilder setLeftImage(int resId) {
        mIvLeft.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
        mIvLeft.setImageResource(resId);
        return this;
    }

    public TitleBuilder setLeftText(String text) {
        mTvLeft.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        mTvLeft.setText(text);
        return this;
    }

    public TitleBuilder setLeftOnClickListener(View.OnClickListener listener) {
        mIvLeft.setOnClickListener(listener);
        mTvLeft.setOnClickListener(listener);
        return this;
    }

    //right
    public TitleBuilder setRightImage(int resId) {
        mIvRight.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
        mIvRight.setImageResource(resId);
        return this;
    }

    public TitleBuilder setRightText(String text) {
        mTvRight.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
        mTvRight.setText(text);
        return this;
    }

    public TitleBuilder setRightOnClickListener(View.OnClickListener listener) {
        if (mIvRight.getVisibility() == View.VISIBLE) {
            mIvRight.setOnClickListener(listener);
        } else if (mTvRight.getVisibility() == View.VISIBLE) {
            mTvRight.setOnClickListener(listener);
        }
        return this;
    }

    public TitleBuilder setBackground(int color) {
        mViewTitle.setBackgroundColor(color);
        return this;
    }

    public View build() {
        return mViewTitle;
    }
}
